﻿// CShowDialog.cpp: 实现文件
//

#include "pch.h"
#include "ScreenShot.h"
#include "afxdialogex.h"
#include "CShowDialog.h"


// CShowDialog 对话框

IMPLEMENT_DYNAMIC(CShowDialog, CDialogEx)

CShowDialog::CShowDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SHOW_DIALOG, pParent)
{
	//获取桌面图像
	GetDesktopWindow()->GetClientRect(&m_DesktopRect);//获取屏幕大小
	CDC* m_hDesktopDC = GetDesktopWindow()->GetDC();//获取屏幕DC
	m_DesktopBitmap.CreateCompatibleBitmap(m_hDesktopDC, m_DesktopRect.Width(), m_DesktopRect.Height());
	m_MemDC.CreateCompatibleDC(m_hDesktopDC);
	//将屏幕DC的内容拷贝到内存DC中
	m_MemDC.SelectObject(&m_DesktopBitmap);
	m_MemDC.BitBlt(0, 0, m_DesktopRect.Width(), m_DesktopRect.Height(), m_hDesktopDC, 0, 0, SRCCOPY);//把桌面图像，内存DC中

	m_hDesktopDC->DeleteDC();//释放屏幕DC
}

CShowDialog::~CShowDialog()
{
}

void CShowDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CShowDialog, CDialogEx)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_MOUSEMOVE()
	ON_WM_SETCURSOR()
	ON_WM_DESTROY()
END_MESSAGE_MAP()


// CShowDialog 消息处理程序


BOOL CShowDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	//获取屏幕大小
	CRect rect;
	//设置对话框大小
	SetWindowPos(&wndTopMost, 0, 0, m_DesktopRect.Width(), m_DesktopRect.Height(), SWP_NOZORDER | SWP_NOMOVE);
	//设置矩形选择框
	m_rectTracker.m_nHandleSize = 5;
	m_rectTracker.m_nStyle = CRectTracker::resizeOutside | CRectTracker::solidLine;
	m_rectTracker.m_rect.SetRect(0, 0, 0, 0);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


void CShowDialog::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	//双缓冲绘图
	CBitmap memBitmap;
	memBitmap.CreateCompatibleBitmap(&dc, m_DesktopRect.Width(), m_DesktopRect.Height());
	CDC memDC;
	memDC.CreateCompatibleDC(&dc);
	memDC.SelectObject(&memBitmap);
	DoubleBufferPaint(&memDC);
	dc.BitBlt(0, 0, m_DesktopRect.Width(), m_DesktopRect.Height(), &memDC, 0, 0, SRCCOPY);


}


void CShowDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
	if (m_rectTracker.HitTest(point) == CRectTracker::hitNothing)//如果点击位置不在矩形选择框内
		m_rectTracker.TrackRubberBand(this, point, TRUE);//绘制矩形选择框
	else
		m_rectTracker.Track(this, point, TRUE);//拖动矩形选择框
	m_rectTracker.m_rect.NormalizeRect();//规范化矩形选择框
	//刷新
	Invalidate();
	CDialogEx::OnLButtonDown(nFlags, point);
}


BOOL CShowDialog::OnEraseBkgnd(CDC* pDC)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	return TRUE;//不擦除背景
}


void CShowDialog::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	if (m_rectTracker.m_rect.PtInRect(point))//如果双击位置在矩形选择框内
	{
		//保存截图
		CBitmap bmp;
		bmp.CreateCompatibleBitmap(&m_MemDC, m_rectTracker.m_rect.Width(), m_rectTracker.m_rect.Height());
		CDC tempDC;
		tempDC.CreateCompatibleDC(&m_MemDC);
		tempDC.SelectObject(&bmp);
		tempDC.BitBlt(0, 0, m_rectTracker.m_rect.Width(), m_rectTracker.m_rect.Height(), &m_MemDC, m_rectTracker.m_rect.left, m_rectTracker.m_rect.top, SRCCOPY);
		//生成文件名
		CString str;
		SYSTEMTIME sys;
		GetLocalTime(&sys);
		str.Format(_T("%d-%d-%d-%d-%d-%d.png"), sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond);
		//保存
		CImage image;
		image.Attach(bmp);
		image.Save(str);
		image.Detach();
		//设置到剪贴板
		if (OpenClipboard())
		{
			EmptyClipboard();//清空剪贴板
			SetClipboardData(CF_BITMAP, bmp);//设置剪贴板数据
			CloseClipboard();//关闭剪贴板
		}

		//关闭对话框
		EndDialog(0);
	}
	CDialogEx::OnLButtonDblClk(nFlags, point);
}


void CShowDialog::OnMouseMove(UINT nFlags, CPoint point)
{
	Invalidate();

	CDialogEx::OnMouseMove(nFlags, point);
}

void CShowDialog::DoubleBufferPaint(CDC* pDC)
{
	pDC->BitBlt(0, 0, m_DesktopRect.Width(), m_DesktopRect.Height(), &m_MemDC, 0, 0, SRCCOPY);//从内存dc复制到dc
	//绘制橡皮筋框
	CPen tempPen(PS_SOLID, 1, RGB(0, 174, 255));
	m_rectTracker.Draw(pDC, &tempPen);

	//绘制放大镜
	//获取鼠标位置
	CPoint point;
	GetCursorPos(&point);
	//在矩形区域获取矩形区域图像
	pDC->SetStretchBltMode(COLORONCOLOR);//设置拉伸模式
	pDC->StretchBlt(point.x + 10, point.y + 30, 100, 100, &m_MemDC, point.x - 25, point.y - 25, 50, 50, SRCCOPY);
	//矩形中心画个十字
	pDC->MoveTo(point.x + 60, point.y + 30 + 2);
	pDC->LineTo(point.x + 60, point.y + 130 - 2);
	pDC->MoveTo(point.x + 10 + 2, point.y + 80);
	pDC->LineTo(point.x + 110 - 2, point.y + 80);
	//画矩形边框
	CRect rect(point.x + 10, point.y + 30, point.x + 110, point.y + 130);
	pDC->Draw3dRect(rect, RGB(0, 0, 0), RGB(0, 0, 0));
	//设置字体
	CFont font;
	font.CreatePointFont(100, _T("微软雅黑"));
	CFont* pOldFont = pDC->SelectObject(&font);
	//设置颜色
	pDC->SetTextColor(RGB(255, 255, 255));
	//显示宽高
	CString str;
	str.Format(_T("%d x %d"), m_rectTracker.m_rect.Width(), m_rectTracker.m_rect.Height());
	pDC->TextOutW(point.x + 10, point.y + 130, str);
	//显示颜色
	COLORREF color = m_MemDC.GetPixel(point.x, point.y);
	str.Format(_T("RGB:(%d,%d,%d)"), GetRValue(color), GetGValue(color), GetBValue(color));
	pDC->TextOutW(point.x + 10, point.y + 148, str);
	pDC->SelectObject(pOldFont);
}


BOOL CShowDialog::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	//获取鼠标坐标
	CPoint point;
	GetCursorPos(&point);
	//设置鼠标形状
	if (m_rectTracker.HitTest(point) == CRectTracker::hitMiddle)
		SetCursor(LoadCursor(NULL, IDC_SIZEALL));
	else if (m_rectTracker.HitTest(point) == CRectTracker::hitTop || m_rectTracker.HitTest(point) == CRectTracker::hitBottom)
		SetCursor(LoadCursor(NULL, IDC_SIZENS));
	else if (m_rectTracker.HitTest(point) == CRectTracker::hitLeft || m_rectTracker.HitTest(point) == CRectTracker::hitRight)
		SetCursor(LoadCursor(NULL, IDC_SIZEWE));
	else if (m_rectTracker.HitTest(point) == CRectTracker::hitTopLeft || m_rectTracker.HitTest(point) == CRectTracker::hitBottomRight)
		SetCursor(LoadCursor(NULL, IDC_SIZENWSE));
	else if (m_rectTracker.HitTest(point) == CRectTracker::hitTopRight || m_rectTracker.HitTest(point) == CRectTracker::hitBottomLeft)
		SetCursor(LoadCursor(NULL, IDC_SIZENESW));
	else
		SetCursor(LoadCursor(NULL, IDC_ARROW));
	return TRUE;
	/*return CDialogEx::OnSetCursor(pWnd, nHitTest, message);*/
}


void CShowDialog::OnDestroy()
{
	CDialogEx::OnDestroy();

	m_DesktopBitmap.DeleteObject();
	m_MemDC.DeleteDC();
}
